package com.cwits.wifi.update;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class UpDateTool {
	public static HashMap<String, String> parse(InputStream in){
    	HashMap<String, String> map = null;
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(false);
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(in, "utf-8");
			int eventType = parser.getEventType();
			String key = "";
			StringBuffer buffer = null;
			while(eventType!=XmlPullParser.END_DOCUMENT){
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					map = new HashMap<String, String>();
					break;
				case XmlPullParser.END_TAG:
					if("country".equals(parser.getName())){
						map.put(key, buffer.toString());	
					}
					break;
				case XmlPullParser.START_TAG:
					if("country".equals(parser.getName())){
						key = parser.getAttributeValue(null,"id");
						buffer = new StringBuffer();
					}else if("item".equals(parser.getName())){
						buffer.append(parser.nextText()).append("\n");
					}
					break;
				default:
					break;
				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
    }

}
